#!/usr/bin/python
# label: { 
#     url: url,
#     stuffs: 
#     {
#        item : { pattern:pattern, message:message, expression:[lambda, p1, p2], matched:[ val, ref ] },
#        ...
#     },
#     contact:[ from, to, topic ]
# }
import re
import logging
from google.appengine.ext import ndb
import datetime

class Predef:

    doors = [ "Running Items", "Reserved Items" ]

    url_='http://'

    topic_='A notification from acqu-center.'

    @staticmethod
    def genMessage(o,n):
        return '{0}<p>{1}</p>'.format(o,n)

    @staticmethod
    def atoi(s):
        try:
            i=int(s)
        except ValueError:
            i=-1
        return i

    @staticmethod
    def isValidEmail(s):
        if s is not None:
            m = re.search(r'\w+@\w+\.\w+',s)
            if m is not None:
                return s
        else:
            raise TypeError,'inValid Email address'

    @staticmethod
    def bare_jid(sender):
        return sender.split('/')[0]

'''
    pattern='pattern'
    message='message'
    expression='expression'
    matched='matched'
    stuffs='stuffs'
    url='url'
    pattern='pattern'
    message='message'
    contact='contact'

    url1_='https://'

    expression_=['1==1', '0', '0']

    matched_= [ '0','0' ]
    #contact_=['acqu-center <acqu.center@gmail.com>', 'acqu-center@googlegroups.com', 'A notification from acqu-center.']
    sender_='acqu-center <acqu.center@gmail.com>'

    xmpp='gtalk'
    facebook='facebook'
    twitter='twitter'
    emails='emails'
    labels='labels'

    @staticmethod
    def isValidHttp(s):
        return (s.startswith(Predef.url_) or s.startswith(Predef.url1_))

    @staticmethod
    def checkMatched(e):
        for i in range(len(e)):
            if e[i]=='':
                e[i]=Predef.matche_[i]
        return e

    @staticmethod
    def checkExpression(e):
        for i in range(len(e)):
            if e[i]=='':
                e[i]=Predef.expression_[i]
        return e


'''

class EmailProperty(ndb.StringProperty):
    def _validate(self,value):
        Predef.isValidEmail(value)

